package com.example.controller;
import com.example.common.Result;
import com.example.pojo.RoomImage;

import com.example.service.RoomImageService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roomImage")
public class RoomImageController {
    @Resource
    private RoomImageService roomImgService;

    /**
     * 添加房间图片
     * @param roomImage
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody RoomImage roomImage) {
        roomImgService.add(roomImage);
        return Result.success(roomImage);
    }

    /**
     * 修改房间图片
     * @param roomImage
     * @return
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody RoomImage roomImage) {
        roomImgService.updateById(roomImage);
        return Result.success(roomImage);
    }

    /**
     * 删除房间图片
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteById(@RequestParam Integer id) {
        roomImgService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除房间
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        roomImgService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 查询所有房间
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(RoomImage roomImage) {
        List<RoomImage> list = roomImgService.selectAll(roomImage);
        return Result.success(list);
    }

    /**@PathVariable的查询方式/selectById/1(/..)
     * 根据id查询图片
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        List<RoomImage> list = roomImgService.selectById(id);
        if (list.size()>0)
        return Result.success(list);
        else {
            return Result.error("401","未找到图片");
        }
    }

    /**@RequestParam的查询方式/selectPage?pageNum=1(&...=...)
     * @RequestParam(defaultValue = "1")默认值为1
     *@RequestParam(required = false)不是必须要传参
     * 分页查询
     */
    @GetMapping("/SelectPage")
    public Result selectPage(RoomImage roomImage,@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize) {
        PageInfo<RoomImage> pageInfo= roomImgService.selectPage(roomImage,pageNum,pageSize);
        return Result.success(pageInfo);
    }
}
